package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsModelDto;
import com.castle.fortress.admin.cms.entity.CmsModelEntity;
import com.castle.fortress.admin.cms.mapper.CmsModelMapper;
import com.castle.fortress.admin.cms.service.CmsModelService;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms模型配置表 服务实现类
 *
 * @author castle
 * @since 2022-03-02
 */
@Service
public class CmsModelServiceImpl extends ServiceImpl<CmsModelMapper, CmsModelEntity> implements CmsModelService {
    /**
    * 获取查询条件
    * @param cmsModelDto
    * @return
    */
    public QueryWrapper<CmsModelEntity> getWrapper(CmsModelDto cmsModelDto){
        QueryWrapper<CmsModelEntity> wrapper= new QueryWrapper();
        if(cmsModelDto != null){
            CmsModelEntity cmsModelEntity = ConvertUtil.transformObj(cmsModelDto,CmsModelEntity.class);
            wrapper.like(cmsModelEntity.getId() != null,"id",cmsModelEntity.getId());
            wrapper.like(StrUtil.isNotEmpty(cmsModelEntity.getTitle()),"title",cmsModelEntity.getTitle());
            wrapper.like(StrUtil.isNotEmpty(cmsModelEntity.getCode()),"code",cmsModelEntity.getCode());
            wrapper.like(cmsModelEntity.getStatus() != null,"status",cmsModelEntity.getStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<CmsModelDto> pageCmsModel(Page<CmsModelDto> page, CmsModelDto cmsModelDto) {
        QueryWrapper<CmsModelEntity> wrapper = getWrapper(cmsModelDto);
        Page<CmsModelEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsModelEntity> cmsModelPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsModelDto> pageDto = new Page(cmsModelPage.getCurrent(), cmsModelPage.getSize(),cmsModelPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsModelPage.getRecords(),CmsModelDto.class));
        return pageDto;
    }

    @Override
    public IPage<CmsModelDto> pageCmsModelExtends(Page<CmsModelDto> page, CmsModelDto cmsModelDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        CmsModelEntity cmsModelEntity = ConvertUtil.transformObj(cmsModelDto,CmsModelEntity.class);
        List<CmsModelEntity> cmsModelList=baseMapper.extendsList(pageMap,cmsModelEntity);
        Long total = baseMapper.extendsCount(cmsModelEntity);
        Page<CmsModelDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(cmsModelList,CmsModelDto.class));
        return pageDto;
    }
    @Override
    public CmsModelDto getByIdExtends(Long id){
        CmsModelEntity  cmsModelEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(cmsModelEntity,CmsModelDto.class);
    }

    @Override
    public List<CmsModelDto> listCmsModel(CmsModelDto cmsModelDto){
        QueryWrapper<CmsModelEntity> wrapper = getWrapper(cmsModelDto);
        List<CmsModelEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsModelDto.class);
    }

    @Override
    public boolean dropTable(String tbName) {
        return false;
    }

    @Override
    public RespBody checkColumnRepeat(CmsModelDto dto) {
        //校验code
        QueryWrapper<CmsModelEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("code",dto.getCode());
        List<CmsModelEntity> list = baseMapper.selectList(queryWrapper);
        if(list != null && !list.isEmpty()){
            //新增
            if(dto.getId() == null){
                return RespBody.fail(BizErrorCode.CMS_CODE_EXIST_ERROR);
                //修改
            }else{
                for(CmsModelEntity entity:list){
                    if(!entity.getId().equals(dto.getId())){
                        return RespBody.fail(BizErrorCode.CMS_CODE_EXIST_ERROR);
                    }
                }
            }
        }
        return RespBody.data("校验通过");
    }

    @Override
    public CmsModelDto getByArticleId(Long id) {
        return ConvertUtil.transformObj(baseMapper.getByArticleId(id),CmsModelDto.class);
    }

    @Override
    public CmsModelDto getByCode(String code) {
        return ConvertUtil.transformObj(baseMapper.getByCode(code),CmsModelDto.class);
    }
}

